const DB = wx.cloud.database().collection("aid_news")
let like = ""
let ID = ""
let content = ""
let pinglun = []
Page({
  data: {
    news_detail:[],
    news_detail_content:[],
    like_imgUrl: "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/aid_news_footer/%E7%82%B9%E8%B5%9E.png?sign=5db6e7b4c0dc5d18206e2aafa4f3a45f&t=1589511275",
    comment:[], //评论数组,
    content:"",
    username: '',
    hpost_comment:true,

  },
  // 点击发表评论，显示内容
  post_comment: function (e) {
    this.setData({
      hpost_comment: !this.data.hpost_comment,
    })
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    ID = options.id;
    console.log("详情页接收的id",ID);
    DB.doc(ID).get().then(res => {
      console.log("详情页成功",res)
      like = res.data.like
      console.log(like)
      this.setData({
        like_imgUrl:like ? "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/aid_news_footer/%E5%B7%B2%E7%82%B9%E8%B5%9E.png?sign=1613b2d666cface40e453154c52cb7bc&t=1589511335"
          :"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/aid_news_footer/%E7%82%B9%E8%B5%9E.png?sign=5db6e7b4c0dc5d18206e2aafa4f3a45f&t=1589511275",
          comment : res.data.comment,
        })
    })
    .catch(res => {
      console.log("失败", res)
    })
    DB.where({
      _id:ID
    })
    .get()
      .then(res => {
        this.setData({
          news_detail: res.data,
          news_detail_content: res.data[0].news_content.split("|"),
        })

      })
      .catch(res => {
        console.log("失败", res)
      })
  },
  // 收藏点击
  clickLike(){
    this.setData({
      like_imgUrl:like ? "https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/aid_news_footer/%E7%82%B9%E8%B5%9E.png?sign=5db6e7b4c0dc5d18206e2aafa4f3a45f&t=1589511275"
      :"https://6d61-main-wc2xz-1259126940.tcb.qcloud.la/sujin/aid_news/aid_news_footer/%E5%B7%B2%E7%82%B9%E8%B5%9E.png?sign=1613b2d666cface40e453154c52cb7bc&t=1589511335",
})
    like = !like
    wx.cloud.callFunction({
      name:"aid_features",
      data:{
        action:"like",
        id:ID,
        like:like
      }
    }).then(res=>{
      console.log("改变点赞状态成功",res)
    })
    .catch(res=>{
      console.log("改变点赞状态失败",res)
    })
  },
 // 获取用户输入的评论内容
getContent(event){
  this.setData({
    content:event.detail.value
  })
},
// 获取用户头像昵称等信息
onGotUserInfo: function (e) {
  console.log("用户昵称",e);
  this.setData({
    username: e.detail.userInfo.nickName,
  })
},

// 发表评论
publish(){
  let username = this.data.username
  let content = this.data.content
  if(content.length < 4){
    wx.showToast({
      icon:"none",
      title: '评论太短了',
    })
    return
  }
  let commentItem = {}
  commentItem.name = username
  commentItem.content = content
  let commentArr = this.data.comment
  commentArr.push(commentItem)
  console.log("添加后的评论数组",commentArr)
  wx.showLoading({
    title: '发表中···',
  })
  wx.cloud.callFunction({
    name:"aid_features",
    data:{
      action:"news_publish",
      id:ID,
      comment:commentArr
    }
  }).then(res => {
    console.log("发表评论成功",res)
    this.setData({
      comment:commentArr,
      content:""
    })
    wx.hideLoading({
      complete: (res) => {},
    })
  }).catch(res => {
    console.log("发表评论失败",res)
    wx.hideLoading({
      complete: (res) => {},
    })
  })
},
})
